package com.ruoyi.plus.web.service;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.framework.manager.factory.AsyncFactory;
import com.ruoyi.framework.web.service.SysLoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

@Slf4j
@Primary
@Component
public class SysLoginServicePlus extends SysLoginService {

    @Autowired
    UserDetailsServiceImplPlus userDetailsService;

    /**
     * app通过手机号验证码登录
     * @param phone 手机号
     * @return 登录结果
     */
    public LoginUser loginByPhone(String phone){
        try{
            LoginUser loginUser = (LoginUser)userDetailsService.loadUserByPhone(phone);
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(phone, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
            // 生成token
            return loginUser;
        }catch (Exception e){
            log.error(e.getMessage(),e);
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(phone, Constants.LOGIN_FAIL, e.getMessage()));
            return null;
        }
    }
}
